package com.elm.controller;

import com.elm.common.ApiResponse;
import com.elm.po.DeliveryAddress;
import com.elm.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api")
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService addressService;

    @GetMapping("/addresses/{userId}/user")
    public ApiResponse<List<DeliveryAddress>> getDeliveryAddressByUserId(@PathVariable int userId) {
    	try {
    		List<DeliveryAddress> createdDeliveryAddress=addressService.getDeliveryAddressByUserId(userId);
//    		if(createdDeliveryAddress.size() == 0) {
//    			return ApiResponse.error("500", "获取用户地址失败");
//    		}
    		return ApiResponse.success(createdDeliveryAddress);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @PostMapping("/addresses")
    public ApiResponse<DeliveryAddress>  saveDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress) {
    	try {
    		DeliveryAddress createdDeliveryAddress=addressService.saveDeliveryAddress(deliveryAddress);
    		if(createdDeliveryAddress == null) {
    			return ApiResponse.error("500", "保存用户地址失败");
    		}
    		return ApiResponse.success(createdDeliveryAddress);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @GetMapping("/addresses/{daId}")
    public ApiResponse<DeliveryAddress> getDeliveryAddressById(@PathVariable Integer daId) {
    	try {
    		DeliveryAddress createdDeliveryAddress=addressService.getDeliveryAddressById(daId);
    		if(createdDeliveryAddress == null) {
    			return ApiResponse.error("500", "获取地址失败");
    		}
    		return ApiResponse.success(createdDeliveryAddress);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @PutMapping("/addresses/update")
    public ApiResponse<DeliveryAddress> updateDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress) {
    	try {
    		DeliveryAddress createdDeliveryAddress=addressService.updateDeliveryAddress(deliveryAddress);
    		if(createdDeliveryAddress == null) {
    			return ApiResponse.error("500", "更新地址失败");
    		}
    		return ApiResponse.success(createdDeliveryAddress);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @DeleteMapping("/addresses/{daId}")
    public ApiResponse<Integer> removeDeliveryAddress(@PathVariable Integer daId) {
    	try {
    		int result = addressService.removeDeliveryAddress(daId);
    		if(result == 0) {
    			return ApiResponse.error("500", "删除地址失败");
    		}
    		return ApiResponse.success(result);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @PutMapping("/default/{daId}")
    public ApiResponse<DeliveryAddress> setDefaultAddress(@PathVariable Integer daId) {
    	try {
    		DeliveryAddress createdDeliveryAddress=addressService.setDefaultAddress(daId);
    		if(createdDeliveryAddress == null || createdDeliveryAddress.getIsDefault()==0) {
    			return ApiResponse.error("500", "更新地址失败");
    		}
    		return ApiResponse.success(createdDeliveryAddress);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }
}